package com.zoe.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Author Zoe
 * @Date: 2020/11/24
 * @Description
 */
@Aspect
@Component
public class MyAop {
    @Pointcut("@annotation(com.zoe.annotaion.LogAnnotation)")
//    @Pointcut("execution(* com.zoe.service.MathService.*(..))")
    public void log() {

    }

    @Around("log()")
    public Object around(ProceedingJoinPoint pjp)throws Throwable  {
        System.out.println("环绕通知前");
        //定义返回值
        Object rtValue = null;
        try {
            //获取方法执行所需的参数
            Object[] args = pjp.getArgs();
            //执行方法
            rtValue = pjp.proceed(args);
        }catch(Throwable e) {
            e.printStackTrace();
        }finally {
        }
        System.out.println("环绕通知后");
        return rtValue;
    }

    @Before("log()")
    public void before(JoinPoint joinPoint) {
        System.out.println("方法名：" + joinPoint.getSignature().getName() + "  --前置通知  --参数列表：" + Arrays.toString(joinPoint.getArgs()));
    }

    @After("log()")
    public void after() {
        System.out.println("后置通知");
    }

    @AfterReturning(value = "log()", returning = "o")
    public void afterReturning(Object o) {
        System.out.println("返回通知 -- 返回的结果是：" + o);
    }

    @AfterThrowing(value = "log()", throwing = "e")
    public void afterThrowing(Exception e) {
        System.out.println("异常通知 -- 异常结果是" + e.getMessage());
    }
}
